<template>
  <view class="cartFooter">
    <radio :checked="flag" @click="allRadioClick" color="#ffbd27">多选</radio>
    <view class="total">合计：<text>￥{{ selectedPrice }}</text></view>
    <view class="payment" @click="settlement">{{ selectedSum ? `结算(${selectedSum})` : '不可结算' }}</view>
  </view>
</template>

<script>
  import { mapState, mapGetters, mapMutations } from 'vuex'
  export default {
    name:"my-settle",
    computed: {
      ...mapGetters('m_cart', ['flag', 'selectedGoods', 'selectedSum', 'selectedPrice']),
      ...mapGetters('m_user', ['longAddress']),
      ...mapState('m_user', ['token', 'address'])
    },
    data() {
      return {
        second: 3,
        timer: ''
      };
    },
    methods: {
      ...mapMutations('m_cart', ['changeAllState']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      allRadioClick(){
        this.changeAllState()
      },
      settlement(){
        if(!this.selectedSum) return
        if(!this.token) return this.delayShowTitle()
        if(!this.longAddress) return uni.$showMsg('请选择收货地址!')
        // this.createOrder() // 创建订单
      },
      delayShowTitle(){
        this.showTip(this.second)
        this.timer = setInterval(() => {
          this.second--
          if(this.second <= 0){
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  openType: 'switchTab',
                  from: '/pages/cart/cart'
                })
              }
            })
            this.second = 3
            return
          }
          this.showTip(this.second)
        }, 1000)
      },
      showTip(n){
        uni.showToast({
          icon: 'none',
          title: '未登录,' +n+ 's后跳转登录页',
          mask: true,
          duration: 1500
        })
      },
      // 创建订单
      // async createOrder(){
      //   const query = {
      //     order_price: 0.01,
      //     consignee_addr: this.address,
      //     goods: this.selectedGoods.map(item => ({goods_id: item.goods_id, goods_number: item.goods_count, goods_price: item.goods_price}))
      //   }
      //   const { data: res1 } = await uni.$http.post('/api/public/v1/my/orders/create', query)
      //   const orderNumber = "GD20180507000000000110"
      //   const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
      //   const payInfo = res2.message.pay
      //   const [err, succ] = await uni.requestPayment(payInfo)
      //   if (err) return uni.$showMsg('订单未支付！')
      //   const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
      //   uni.showToast({
      //    title: '支付完成！',
      //    icon: 'success'
      //   })
      // }
    }
  }
</script>

<style lang="scss">
  .cartFooter {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 100rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #ffffff;
    z-index: 999;
    radio {
      transform: scale(0.8);
    }
    .total {
      text {
        color: #ffbd27;
      }
    }
    .payment {
      min-width: 200rpx;
      background-color: #ffbd27;
      text-align: center;
      line-height: 100rpx;
      color: #ffffff;
    }
  }
</style>